#pragma once
#include <set>
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/chrono.hpp>
#include "GSE_Timer.h"

using namespace std;
using namespace boost::chrono;

namespace GSE
{
	class CTimerWrapper
	{
	public:
		CTimerWrapper(uint uMilliSeconds,ITimerCallback *);
		~CTimerWrapper() {}

		void SetScheduleTime(system_clock::time_point StartTime);
		void SetScheduleDuration(system_clock::duration m_uMinutes);
		steady_clock::time_point GetStartTime();
		steady_clock::time_point GetNextTime();
		void Trigger();


	private:
		steady_clock::time_point	m_StartTime;
		steady_clock::time_point	m_NextTime;
		steady_clock::duration		m_Delay;

		system_clock::time_point	m_ScheduleTime;
		system_clock::duration		m_ScheduleDelay;
		ITimerCallback * 			m_pCallback;
		bool						m_IsSchedule;
	};

	class compare
	{
	public:
		bool operator()(boost::shared_ptr<CTimerWrapper> t1, boost::shared_ptr<CTimerWrapper> t2) const
		{
			return t1->GetNextTime() < t2->GetNextTime();
		}
	};

	class CTimerModule : public ITimer
	{
	public:
		virtual void CreateTimer( uint uMilliSeconds, ITimerCallback * lpCallback );
		virtual void CreateSchedule(tm * _tStartTime,uint uMinutes,ITimerCallback * );
		virtual void KillTimer(const char * strTimerName);
		virtual uint Run();

	private:
		set<boost::shared_ptr<CTimerWrapper>,compare> m_setTimers;
		map<const char *,boost::shared_ptr<CTimerWrapper>> m_indexTimerName;

	public:
		static CTimerModule * m_pInstance;
	};
}